home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch18 / Animated.frm (.txt) next >
Visual Basic Form  |  1999-07-10  |  12KB  |  379 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAnimated 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Animated"
  6.    ClientHeight    =   5310
  7.    ClientLeft      =   1410
  8.    ClientTop       =   570
  9.    ClientWidth     =   6870
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5310
  24.    ScaleWidth      =   6870
  25.    Begin VB.Frame Frame1 
  26.       Caption         =   "Pre-Rotations"
  27.       Height          =   1335
  28.       Index           =   0
  29.       Left            =   5400
  30.       TabIndex        =   10
  31.       Top             =   0
  32.       Width           =   1455
  33.       Begin VB.CheckBox chkXY 
  34.          Caption         =   "XY Plane"
  35.          Height          =   255
  36.          Left            =   120
  37.          TabIndex        =   13
  38.          Top             =   240
  39.          Value           =   1  'Checked
  40.          Width           =   1215
  41.       End
  42.       Begin VB.CheckBox chkXZ 
  43.          Caption         =   "XZ Plane"
  44.          Height          =   255
  45.          Left            =   120
  46.          TabIndex        =   12
  47.          Top             =   600
  48.          Width           =   1215
  49.       End
  50.       Begin VB.CheckBox chkYZ 
  51.          Caption         =   "YZ Plane"
  52.          Height          =   255
  53.          Left            =   120
  54.          TabIndex        =   11
  55.          Top             =   960
  56.          Width           =   1215
  57.       End
  58.    End
  59.    Begin VB.Frame Frame1 
  60.       Caption         =   "Post-Rotations"
  61.       Height          =   1335
  62.       Index           =   1
  63.       Left            =   5400
  64.       TabIndex        =   3
  65.       Top             =   2040
  66.       Width           =   1455
  67.       Begin VB.TextBox txtXW2 
  68.          Height          =   285
  69.          Left            =   600
  70.          MaxLength       =   6
  71.          TabIndex        =   6
  72.          Text            =   "0.2"
  73.          Top             =   240
  74.          Width           =   735
  75.       End
  76.       Begin VB.TextBox txtYW2 
  77.          Height          =   285
  78.          Left            =   600
  79.          MaxLength       =   6
  80.          TabIndex        =   5
  81.          Text            =   "0.1"
  82.          Top             =   600
  83.          Width           =   735
  84.       End
  85.       Begin VB.TextBox txtZW2 
  86.          Height          =   285
  87.          Left            =   600
  88.          MaxLength       =   6
  89.          TabIndex        =   4
  90.          Text            =   "0.0"
  91.          Top             =   960
  92.          Width           =   735
  93.       End
  94.       Begin VB.Label Label1 
  95.          Caption         =   "X"
  96.          Height          =   255
  97.          Index           =   9
  98.          Left            =   240
  99.          TabIndex        =   9
  100.          Top             =   240
  101.          Width           =   255
  102.       End
  103.       Begin VB.Label Label1 
  104.          Caption         =   "Y"
  105.          Height          =   255
  106.          Index           =   10
  107.          Left            =   240
  108.          TabIndex        =   8
  109.          Top             =   600
  110.          Width           =   255
  111.       End
  112.       Begin VB.Label Label1 
  113.          Caption         =   "Z"
  114.          Height          =   255
  115.          Index           =   11
  116.          Left            =   240
  117.          TabIndex        =   7
  118.          Top             =   960
  119.          Width           =   255
  120.       End
  121.    End
  122.    Begin VB.TextBox txtD 
  123.       Height          =   285
  124.       Left            =   6000
  125.       TabIndex        =   2
  126.       Text            =   "3"
  127.       Top             =   1560
  128.       Width           =   735
  129.    End
  130.    Begin VB.CommandButton cmdGo 
  131.       Caption         =   "Go"
  132.       Default         =   -1  'True
  133.       Height          =   375
  134.       Left            =   5760
  135.       TabIndex        =   1
  136.       Top             =   3600
  137.       Width           =   735
  138.    End
  139.    Begin VB.PictureBox picCanvas 
  140.       AutoRedraw      =   -1  'True
  141.       Height          =   5295
  142.       Left            =   0
  143.       ScaleHeight     =   349
  144.       ScaleMode       =   3  'Pixel
  145.       ScaleWidth      =   349
  146.       TabIndex        =   0
  147.       Top             =   0
  148.       Width           =   5295
  149.    End
  150.    Begin VB.Label Label1 
  151.       Caption         =   "D"
  152.       Height          =   255
  153.       Index           =   12
  154.       Left            =   5640
  155.       TabIndex        =   14
  156.       Top             =   1560
  157.       Width           =   255
  158.    End
  159. Attribute VB_Name = "frmAnimated"
  160. Attribute VB_GlobalNameSpace = False
  161. Attribute VB_Creatable = False
  162. Attribute VB_PredeclaredId = True
  163. Attribute VB_Exposed = False
  164. Option Explicit
  165. ' Location of focus point.
  166. Private Const FocusX = 0#
  167. Private Const FocusY = 0#
  168. Private Const FocusZ = 0#
  169. ' The points.
  170. Private NumPoints As Integer
  171. Private Points() As Point4D
  172. ' The segments.
  173. Private NumSegments As Integer
  174. Private Segments() As Segment4D
  175. Private Running As Boolean
  176. ' Animate the hypercube.
  177. Private Sub Animate(ByVal pic As PictureBox)
  178. Const Dtheta = PI / 40
  179. Dim xy_rot As Single
  180. Dim xz_rot As Single
  181. Dim yz_rot As Single
  182. Dim xw2_rot As Single
  183. Dim yw2_rot As Single
  184. Dim zw2_rot As Single
  185. Dim XY(1 To 5, 1 To 5) As Single
  186. Dim XZ(1 To 5, 1 To 5) As Single
  187. Dim YZ(1 To 5, 1 To 5) As Single
  188. Dim XW2(1 To 5, 1 To 5) As Single
  189. Dim YW2(1 To 5, 1 To 5) As Single
  190. Dim ZW2(1 To 5, 1 To 5) As Single
  191. Dim S(1 To 5, 1 To 5) As Single
  192. Dim T(1 To 5, 1 To 5) As Single
  193. Dim P(1 To 5, 1 To 5) As Single
  194. Dim M12(1 To 5, 1 To 5) As Single
  195. Dim M34(1 To 5, 1 To 5) As Single
  196. Dim M1_4(1 To 5, 1 To 5) As Single
  197. Dim M56(1 To 5, 1 To 5) As Single
  198. Dim M1_6(1 To 5, 1 To 5) As Single
  199. Dim M_All(1 To 5, 1 To 5) As Single
  200. Dim D As Single
  201. Dim AnimateXY As Boolean
  202. Dim AnimateXZ As Boolean
  203. Dim AnimateYZ As Boolean
  204. Dim next_time As Long
  205.     If Not IsNumeric(txtXW2.Text) Then Exit Sub
  206.     If Not IsNumeric(txtYW2.Text) Then Exit Sub
  207.     If Not IsNumeric(txtZW2.Text) Then Exit Sub
  208.     If Not IsNumeric(txtD.Text) Then Exit Sub
  209.     xw2_rot = CSng(txtXW2.Text)
  210.     yw2_rot = CSng(txtYW2.Text)
  211.     zw2_rot = CSng(txtZW2.Text)
  212.     D = CSng(txtD.Text)
  213.     Screen.MousePointer = vbHourglass
  214.     DoEvents
  215.     ' Prevent overflow errors when drawing lines
  216.     ' too far out of bounds.
  217.     On Error Resume Next
  218.     ' Calculate the matrices that don't change.
  219.     m4XWRotate XW2, xw2_rot
  220.     m4YWRotate YW2, yw2_rot
  221.     m4ZWRotate ZW2, zw2_rot
  222.     ' Calculate the projection matrix.
  223.     m4PerspectiveW P, D
  224.     ' Scale and translate so it looks OK in pixels.
  225.     m4Scale S, 75, -75, 1, 1
  226.     m4Translate T, pic.ScaleWidth / 2, pic.ScaleHeight / 2, 0, 0
  227.     m4MatMultiplyFull M12, P, XW2
  228.     m4MatMultiply M34, YW2, ZW2
  229.     m4MatMultiplyFull M1_4, M12, M34
  230.     m4MatMultiply M56, S, T
  231.     m4MatMultiplyFull M1_6, M1_4, M56
  232.     ' See which rotations we are animating.
  233.     AnimateXY = (chkXY.value = vbChecked)
  234.     AnimateXZ = (chkXZ.value = vbChecked)
  235.     AnimateYZ = (chkYZ.value = vbChecked)
  236.     ' Start the animation.
  237.     Do While Running
  238.         next_time = GetTickCount + 50
  239.         ' Calculate the changing transformations.
  240.         m4XYRotate XY, xy_rot
  241.         m4XZRotate XZ, xz_rot
  242.         m4YZRotate YZ, yz_rot
  243.         m4MatMultiply M12, XY, XZ
  244.         m4MatMultiply M1_4, M12, YZ
  245.         m4MatMultiplyFull M_All, M1_4, M1_6
  246.         If AnimateXY Then xy_rot = xy_rot + Dtheta
  247.         If AnimateXZ Then xz_rot = xz_rot + Dtheta
  248.         If AnimateYZ Then yz_rot = yz_rot + Dtheta
  249.         ' Transform the points.
  250.         ApplyFull M_All
  251.         ' Display the data.
  252.         pic.Cls
  253.         Draw pic
  254.         DoEvents
  255.         WaitTill next_time
  256.     Loop
  257.     Screen.MousePointer = vbDefault
  258. End Sub
  259. ' Add a segment to the lists.
  260. Private Sub AddSegment( _
  261.     ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal w1 As Single, _
  262.     ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, ByVal w2 As Single _
  263. Dim pt1 As Integer
  264. Dim pt2 As Integer
  265.     ' Find the points.
  266.     pt1 = PointNumber(x1, y1, z1, w1)
  267.     pt2 = PointNumber(x2, y2, z2, w2)
  268.     ' Create the segment entry.
  269.     NumSegments = NumSegments + 1
  270.     ReDim Preserve Segments(1 To NumSegments)
  271.     With Segments(NumSegments)
  272.         .pt1 = pt1
  273.         .pt2 = pt2
  274.     End With
  275. End Sub
  276. ' Apply this matrix to the points.
  277. Private Sub Apply(M() As Single)
  278. Dim pt As Integer
  279.     For pt = 1 To NumPoints
  280.         m4Apply Points(pt).coord, M, Points(pt).trans
  281.     Next pt
  282. End Sub
  283. ' Apply this matrix to the points.
  284. Private Sub ApplyFull(M() As Single)
  285. Dim pt As Integer
  286.     For pt = 1 To NumPoints
  287.         m4ApplyFull Points(pt).coord, M, Points(pt).trans
  288.     Next pt
  289. End Sub
  290. ' Draw the segments.
  291. Private Sub Draw(ByVal pic As PictureBox)
  292. Dim seg As Integer
  293.     For seg = 1 To NumSegments
  294.         pic.Line ( _
  295.             Points(Segments(seg).pt1).trans(1), _
  296.             Points(Segments(seg).pt1).trans(2))-( _
  297.             Points(Segments(seg).pt2).trans(1), _
  298.             Points(Segments(seg).pt2).trans(2))
  299.     Next seg
  300. End Sub
  301. ' Find this point's index. If it is not here,
  302. ' create it.
  303. Private Function PointNumber(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal W As Single)
  304. Dim i As Integer
  305.     ' Find the point.
  306.     For i = 1 To NumPoints
  307.         With Points(i)
  308.             If .coord(1) = X And _
  309.                .coord(2) = Y And _
  310.                .coord(3) = Z And _
  311.                .coord(4) = W _
  312.             Then
  313.                 PointNumber = i
  314.                 Exit Function
  315.             End If
  316.         End With
  317.     Next i
  318.     ' We did not find the point. Create it.
  319.     NumPoints = NumPoints + 1
  320.     ReDim Preserve Points(1 To NumPoints)
  321.     With Points(NumPoints)
  322.         .coord(1) = X
  323.         .coord(2) = Y
  324.         .coord(3) = Z
  325.         .coord(4) = W
  326.         .coord(5) = 1#
  327.     End With
  328.     PointNumber = NumPoints
  329. End Function
  330. Private Sub cmdGo_Click()
  331.     If Running Then
  332.         ' Stop it.
  333.         cmdGo.Caption = "Go"
  334.         Running = False
  335.     Else
  336.         cmdGo.Caption = "Stop"
  337.         Running = True
  338.         Animate picCanvas
  339.     End If
  340. End Sub
  341. Private Sub Form_Load()
  342.     ' Create the data.
  343.     CreateData
  344. End Sub
  345. ' Create the hypercube.
  346. Private Sub CreateData()
  347. Dim X As Integer
  348. Dim Y As Integer
  349. Dim Z As Integer
  350. Dim W As Integer
  351.     Screen.MousePointer = vbHourglass
  352.     Refresh
  353.     For X = -1 To 1 Step 2
  354.         For Y = -1 To 1 Step 2
  355.             For Z = -1 To 1 Step 2
  356.                 For W = -1 To 1 Step 2
  357.                     If X = -1 Then _
  358.                         AddSegment _
  359.                             X, Y, Z, W, _
  360.                             1, Y, Z, W
  361.                     If Y = -1 Then _
  362.                         AddSegment _
  363.                             X, Y, Z, W, _
  364.                             X, 1, Z, W
  365.                     If Z = -1 Then _
  366.                         AddSegment _
  367.                             X, Y, Z, W, _
  368.                             X, Y, 1, W
  369.                     If W = -1 Then _
  370.                         AddSegment _
  371.                             X, Y, Z, W, _
  372.                             X, Y, Z, 1
  373.                 Next W
  374.             Next Z
  375.         Next Y
  376.     Next X
  377.     Screen.MousePointer = vbDefault
  378. End Sub
  379.